using System;

namespace CinemaDirector
{
	public class ShotTrack : GlobalTrack
	{
		public CinemaShot[] Shots
		{
			get
			{
				return GetComponentsInChildren<CinemaShot>();
			}
		}

		public override TimelineItem[] TimelineItems
		{
			get
			{
				return GetComponentsInChildren<CinemaShot>();
			}
		}

		public event ShotBeginsEventHandler ShotBegins;

		public override void Initialize()
		{
			elapsedTime = 0f;
			CinemaShot[] shots = Shots;
			foreach (CinemaShot cinemaShot in shots)
			{
				cinemaShot.Initialize();
				if (cinemaShot.Firetime == 0f)
				{
					cinemaShot.Trigger();
				}
				else
				{
					cinemaShot.End();
				}
			}
		}

		public override void UpdateTrack(float time, float deltaTime)
		{
			float num = elapsedTime;
			elapsedTime = time;
			CinemaShot[] shots = Shots;
			foreach (CinemaShot cinemaShot in shots)
			{
				float num2 = cinemaShot.CutTime + cinemaShot.Duration;
				if (num <= cinemaShot.CutTime && elapsedTime >= cinemaShot.CutTime && elapsedTime < num2)
				{
					cinemaShot.Trigger();
					if (this.ShotBegins != null)
					{
						this.ShotBegins(this, new ShotEventArgs(cinemaShot));
					}
				}
				else if (num >= num2 && elapsedTime < num2 && elapsedTime >= cinemaShot.CutTime)
				{
					cinemaShot.Trigger();
					if (this.ShotBegins != null)
					{
						this.ShotBegins(this, new ShotEventArgs(cinemaShot));
					}
				}
				else if (num >= cinemaShot.CutTime && num < num2 && elapsedTime > num2)
				{
					cinemaShot.End();
				}
				else if (num > cinemaShot.CutTime && num < num2 && elapsedTime < cinemaShot.CutTime)
				{
					cinemaShot.End();
				}
			}
		}

		public override void SetTime(float time)
		{
			elapsedTime = time;
			CinemaShot[] shots = Shots;
			foreach (CinemaShot cinemaShot in shots)
			{
				float num = cinemaShot.CutTime + cinemaShot.Duration;
				if (elapsedTime >= cinemaShot.CutTime && elapsedTime < num)
				{
					cinemaShot.Trigger();
					if (this.ShotBegins != null)
					{
						this.ShotBegins(this, new ShotEventArgs(cinemaShot));
					}
				}
				else
				{
					cinemaShot.End();
				}
			}
		}

		public override void Stop()
		{
			CinemaShot[] shots = Shots;
			foreach (CinemaShot cinemaShot in shots)
			{
				cinemaShot.Stop();
			}
		}

		public CinemaShot[] GetSortedShots()
		{
			CinemaShot[] componentsInChildren = GetComponentsInChildren<CinemaShot>();
			Array.Sort(componentsInChildren);
			return componentsInChildren;
		}
	}
}
